package com.aurora.source.authority;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.aurora.source.R;
import com.aurora.source.adapter.CommodityManagementAdapter;
import com.aurora.source.authority.bean.CommodityManagementBean;
import com.aurora.source.authority.presenter.CommodityManagementPresenter;
import com.aurora.source.authority.view.ICommodityManagementView;
import com.aurora.source.base.BaseWhiteToolbarActivity;
import com.aurora.source.center.activity.AddProductInformationActivity;
import com.aurora.source.entity.SysPermissionEntity;
import com.aurora.source.util.GreenDaoHelper;
import com.aurora.source.util.SPUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CommodityManagementActivity extends BaseWhiteToolbarActivity<CommodityManagementPresenter> implements ICommodityManagementView {

    private static final int ADD_PRODUCT = 1001;
    @BindView(R.id.et_seek)
    EditText etSeek;
    @BindView(R.id.iv_seek)
    ImageView ivSeek;
    @BindView(R.id.rv_recyclerView)
    RecyclerView rvRecyclerView;
    private CommodityManagementAdapter commodityManagementAdapter;
    private boolean addProduct;

    @Override
    public CommodityManagementPresenter attach() {
        return new CommodityManagementPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_commodity_management;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        SysPermissionEntity sysPermissionEntity = GreenDaoHelper.getDaoSession().getSysPermissionEntityDao().loadAll().get(0);
        addProduct = sysPermissionEntity.getAddProduct();
        Map<String, String> params = new HashMap<>();
        params.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
        basePresenter.commodityManagement(activity, params);
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        simpleToolbar.setMainTitle("商品管理");
        simpleToolbar.setLeftVisibility(View.VISIBLE);
        simpleToolbar.setRightVisibility(View.VISIBLE);
        simpleToolbar.setRightTitleText("添加商品");
        simpleToolbar.setRightTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!addProduct){
                    showToast("暂无权限");
                    return;
                }
                gotoActivityForResult(AddProductInformationActivity.class,ADD_PRODUCT);
            }
        });

        commodityManagementAdapter = new CommodityManagementAdapter();
        rvRecyclerView.setAdapter(commodityManagementAdapter);
        rvRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    @Override
    public void doBusiness() {

    }



    @Override
    public void onCommodityManagementSuccess(CommodityManagementBean data) {
        commodityManagementAdapter.setDataList(data.getProductResponseModelList());
    }

    @Override
    public void onCommodityManagementFail(String msg) {
        showToast(msg);
    }

    @OnClick({R.id.iv_seek})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_seek:
                Map<String, String> params = new HashMap<>();
                params.put("userId",(String) SPUtils.get(activity, SPUtils.USER_ID, ""));
                params.put("productName",etSeek.getText().toString());
                basePresenter.commodityManagement(activity, params);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==ADD_PRODUCT){
            Map<String, String> params = new HashMap<>();
            params.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
            basePresenter.commodityManagement(activity, params);
        }
    }
}
